//
//  LinkingCardResultView.swift
//  iPhone
//
//  Created by Toj on 11/9/22.
//

import UIKit

enum LinkingCardResultState: Int {
    case success
    case faild
    
    var icon: String {
        switch self {
        case .success:
            return "com_success_green"
        default:
            return "com_failed_red"
        }
    }
    
    var title: String {
        switch self {
        case .success:
            return "Congratulations!\nyou have tied the card successfully."
        case .faild:
            return "Failed to bind the card. \nPlease try again."
        }
    }
}

class LinkingCardResultView: UIView {
    var state: LinkingCardResultState = .success { didSet {
        imageView.image = state.icon.to_image
        titleLabel.text = state.title
    } }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupSubviews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        setupLayoutSubviews()
    }
    
    private lazy var imageView = UIImageView()
    
    private lazy var titleLabel: UILabel = {
        let view = UILabel()
        view.numberOfLines = 0
        view.textAlignment = .center
        view.textColor = "#666666".to_color
        view.font = UIFont.systemFont(ofSize: 14.f_scale)
        return view
    }()
    
    private let okButtonHeight = 32.ui_scale
    private(set) lazy var okButton: UIButton = {
        let view = UIButton()
        view.backgroundColor = "#E8CD8C".to_color
        view.setTitle("OK", for: .normal)
        view.setTitleColor("#775E33".to_color, for: .normal)
        view.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14.f_scale)
        view.cm_cornerRadius = okButtonHeight * 0.5
        return view
    }()
    
    deinit {
        // print("-->toj: LinkingCardResultView " + #function)
    }
}

extension LinkingCardResultView {
    
    private func setupSubviews() {
        
        addSubview(imageView)
        addSubview(titleLabel)
        addSubview(okButton)
    }
    
    private func setupLayoutSubviews() {
        titleLabel.snp.makeConstraints { make in
            make.center.equalTo(self)
        }
        
        imageView.snp.makeConstraints { make in
            make.centerX.equalTo(self)
            make.bottom.equalTo(titleLabel.snp.top).offset(-12.ui_scale)
            make.height.width.equalTo(56.ui_scale)
        }
        
        okButton.snp.makeConstraints { make in
            make.height.equalTo(okButtonHeight)
            make.top.equalTo(titleLabel.snp.bottom).offset(30.ui_scale)
            make.left.right.equalTo(self).inset(16.ui_scale)
        }
    }
}
